package com.huichenghe.xinlvsh01.Ble;

import android.content.Context;
import android.util.Log;

import com.huichenghe.xinlvsh01.DataEntites.CustomRemindEntity;
import com.huichenghe.xinlvsh01.R;
import com.huichenghe.xinlvsh01.Utils.FormatUtils;

import java.io.UnsupportedEncodingException;

/**
 * 自定义提醒类，发送数据和处理返回的数据
 * Created by lixiaoning on 15-12-3.
 */
public class BleDataForCustomRemind extends BleBaseDataManage
{
    public static final String TAG = BleDataForCustomRemind.class.getSimpleName();
    public static final byte toDevice = (byte)0x09;
    public static final byte fromDevice = (byte)0x89;



    /**
     * 向手环发送指令获取自定义提醒内容
     */
    public void getCustomRemind()
    {
        byte[] bytes = new byte[2];
        bytes[0] = (byte)0x00;
        for (int i = 0; i < 8; i ++)
        {
            bytes[1] = (byte)(i & 0xff);

            setMsgToByteDataAndSendToDevice(toDevice, bytes, bytes.length, true);

            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
    CustomRemindEntity entity;
    byte[] hasData = new byte[8];   // 储存提醒编号
    public CustomRemindEntity dealTheValidData(Context mContext, byte[] dataValid)
    {

//        00 00 04 01 0d34 2e 6c16
//        00 01 02 01 1337 61 a716

        if(dataValid.length > 5)    // 长度大于4是有效数据
        {
            if(dataValid[0] == (byte)0x00)  // 第一个字节00代表是读取自定义提醒返回的数据
            {
                Log.i(TAG, "有效的自定义提醒数据：" + FormatUtils.bytesToHexString(dataValid));
                boolean isCustom = false;
                entity = new CustomRemindEntity();
                entity.setNumber((byte)dataValid[1]);   // 设置此对象的编号

                byte type = dataValid[2];               // 题型类型，决定图片的选择

                entity.setType(type);
                if(type == (byte)0x06)
                {
                    isCustom = true;
                }
                int count = dataValid[3] & 0xff;
                entity.setCount(count);   // 提醒时间的个数
                String timeS = "";
                for (int i = 4; i < 4 + count * 2; i += 2)
                {
                    int hour = dataValid[i] & 0xff;
                    int minute = dataValid[i + 1] & 0xff;
                    String a = hour + ":" + minute;
                    timeS = timeS + a;
                }

                entity.setTimes(timeS);
                int ind = 3 + 2 * count + 1;
                byte week = dataValid[ind];
                String weekS = FormatWeeks(mContext, week);          // 构建星期显示格式
                entity.setRepeats(weekS);

                if(isCustom)
                {
                    String cusName = null;
                    int start = ind + 1;
                    int end = dataValid.length - 3;

                    byte[] customByte = new byte[end - start];
                    for (int i = start; i < end - start; i ++ )
                    {
                        customByte[i - start] = dataValid[i];
                    }

                    try {
                        cusName = new String(customByte, "GB2312");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    entity.setCustomName(cusName);
                }


                return entity;
            }



        }



        return null;
    }

    private String FormatWeeks(Context mContext, byte week)
    {
        String ret = "";
        String[] weekName = new String[]
        {
                mContext.getString(R.string.sunday_all),
                mContext.getString(R.string.thursday_all),
                mContext.getString(R.string.monday_all),
                mContext.getString(R.string.wednesday_all),
                mContext.getString(R.string.thursday_all),
                mContext.getString(R.string.friday_all),
                mContext.getString(R.string.saturday_all),
        };

        for (int i = 0; i < 7; i ++)
        {
            if((week >> i & (byte)0x01) == (byte)0x01)  // week依次右移，并做and运算，若结果为0x01,则代表有对应的天
            {
                if(!ret.equals(""))
                {
                    ret += "\\";
                }

                ret += weekName[i];
            }

        }

        return ret;
    }

}
